package com.chrray.position.util.sha256Util;

import com.chrray.position.util.uuid.UUIDUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;
import sun.misc.BASE64Decoder;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author M
 * @date
 */
@Slf4j
public class ShA256Utils {
    /**
     * sha256_HMAC加密
     *
     * @param message 消息
     * @param secret  秘钥
     * @return 加密后字符串
     */
    public static String sha256_HMAC(String message, String secret) {
        try {
            Signature sign = Signature.getInstance("SHA256withRSA");
            secret=secret.replaceAll(" ","");
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(secret));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyf.generatePrivate(priPKCS8);

            sign.initSign(privateKey);
            sign.update(message.getBytes(StandardCharsets.UTF_8));
            return Base64Utils.encodeToString(sign.sign());
        } catch (Exception e) {
            log.error("下单失败",e);
        }

        return "";


    }


    public static Sha256WxPayVo getWxPaySign(String appId, String prepayId, String secret) {
        String timeStamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) + "";
        String nonceStr = UUIDUtils.getUUIDRemoveLine();
        String packageString = "prepay_id=" + prepayId;
        String signType = "RSA";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(appId).append("\n");
        stringBuffer.append(timeStamp).append("\n");
        stringBuffer.append(nonceStr).append("\n");
        stringBuffer.append(packageString).append("\n");
        String paySign = sha256_HMAC(stringBuffer.toString(), secret);
        Sha256WxPayVo vo = new Sha256WxPayVo();
        vo.setNonceStr(nonceStr);
        vo.setPackageString(packageString);
        vo.setPaySign(paySign);
        vo.setTimeStamp(timeStamp);
        vo.setSignType(signType);
        return vo;
    }

    public static void main(String[] args) {
        String key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQChLVd4I/czLvGV" +
                "3/crDGufMhp+udNHaETT/6qOGAEMPQmNR5nKWHayQle4ED4hAROvFScRq1Hp9XRo" +
                "FTuJVUVNmTD9IdZsC1+s1RFWDghhnDmzwxVP5Xsa47g7wwlb50UYEYHrRQYFFp03" +
                "wc75aDIo86BEadmmvVeEStOUC6vtaw9jCogwo4q76uTqHR0vps9Y2UGwOVNx5RKi" +
                "IpnqEZSKo0+6ZvcXhFi7hoJahhq6gdex8jrl/kQD/R6DkxbDlGRrpZLFHfu7yi0e" +
                "5YO4IdvShcyRgzH0gFdNNvMlIhW+cr5l+UR6LmJQEDFFsqoaye6Y9JEZZ8FWlv2j" +
                "uxPlscnFAgMBAAECggEACZ7zQbCj6OI8eX3M+zzaj74l5rqg6fpHYYA5ZhH4vgpG" +
                "r7A+dd2h+DINmmcdzkqKKkFQ+7UGRrjoIe5ih2gXovIqAnIqonhrVZ3CUyizR2BT" +
                "8W8LrqE8H/bH4Ilb3aiIb09cOfb8zQD0kYd4G1sk41znjC3lWBalSrhMuOGZeap6" +
                "F40yWLYd5d/yBCFNES/ey13P2ET51FdiNU9KwXfJ9NP2sOXNWspfMypsEkeRgNMh" +
                "lvV0XClZeFdnSRslNmwI87oLgHZXg/3E3jdqS1+gm/9kDr4kefBhysdNRxFDgRr+" +
                "JHa/kujFmAJaqYXQlNUofWNflNrr5S5iTUDk3DGqAQKBgQDOwz4s2xo8aWvjKZU8" +
                "egwofP/bnUBPGl9pie+h9O6ftlppaEN1TqcxkZ+HjZxgYIF8CwdzxZOEc/PmSXk+" +
                "V70mD8GgiieY2xVU2RQsOfftRqYeVEkxohLvFysISlhJOOJAaI6SRA73rW0luF5H" +
                "6ZhIzzcDD3aw4eGFOdddsuNHEQKBgQDHjxeeezMEPr5cDctVSnvD9sVLgBKILpu+" +
                "QGlo/NKiVbrL061OcO5MfpsenY7pcUGjwrnfoE4cHVQbZnDb+7n3sx8ndwvaFkhr" +
                "pNFO7x4nX1/wUxIA1xU2RSgvBMPfNpyVD2t6nC+c14P7l8TNVwj9DH90Xxx3lQSO" +
                "Kc+uA0NfdQKBgFPnkTycsTngwgJOJpyDHpnzl9SkLKutFRj7+72qaodvdaMex3k8" +
                "MMcBxE463kGSCBmxVBXcELAIaeqC2Gu/KSff7wBZ8BWsmixzZydHYt/r8o+OPqGg" +
                "lrRln3XezZLxPYdLpseeTMCfpRJQjhn6pmytgjKAvPasw1rQDSCd7nMxAoGAckmL" +
                "8FKEVDEO2i94PN+MZB2GvoeZlW6D+YBwHhV1GE1aznLrNRpOD3cOpYj8mMspGX1i" +
                "8CSht05ApfSUPBg9WBO4iuSN4BeU4DkjOj6NmWTwz/TzQZHsbwlKSytCdiqToHaV" +
                "SkH6rqtqxgMUTblDx5CJyf7SkdXBJx3g97qcsiUCgYBh5hDeaTBs2TNyfjmUaoE2" +
                "mcMg6j9kf/GED/KJdRngHelUApP+IvdXI4u6LqTXFxPtAzrAC37Ty9aJCRDCboRl" +
                "L7LkD1VfvfdDPXCVhPP4T2J+lncp2wOPeGDSTJYaiaPs2lhnPhi2/vyy3KQbfLIc" +
                "GVpXxEBBjjO/DZAU2OzWCw==";
        String s = ShA256Utils.sha256_HMAC("wx98f7e09303666e2b\n1630908677\n7172eb16225e4ee5b7a9d1202afde3f4\nprepay_id=wx06141117582848475149afec9ec6b10000\n", key);
        System.out.println(s);
    }
}
